package com.link2room.aeriabots.facade.impl;

import com.link2room.aeriabots.dto.param.base.WxJspSignParamDto;
import com.link2room.aeriabots.dto.result.api.WxJspSignApiDto;
import com.link2room.aeriabots.dto.result.base.WxOfficialAccountsMenuDto;
import com.link2room.aeriabots.facade.IWxOfficialAccountsMenuFacadeService;
import com.link2room.aeriabots.service.IMenuService;
import com.link2room.common.constant.DefaultValue;
import com.link2room.common.enums.BaseSysExceptionEnum;
import com.link2room.common.exception.SysException;
import com.link2room.common.jpa.result.QueryResult;
import com.link2room.common.service.ICommonService;
import com.link2room.common.util.ClassConverUtil;
import com.link2room.common.util.SHA1;
import com.link2room.common.util.StringUtil;
import com.link2room.aeriabots.dto.param.api.WxMenuApiParamDto;
import com.link2room.aeriabots.dto.result.api.WxMenuResDto;
import com.link2room.aeriabots.entity.WxCertificate;
import com.link2room.aeriabots.entity.WxOfficialAccountsMenu;
import com.link2room.enums.exception.AeriabotsSysExceptionEnum;
import com.tencent.client.OfficialAccountClient;
import com.tencent.protocol.officialAccount.menu.MenuButton;
import com.tencent.protocol.officialAccount.menu.create.MenuCreateResData;
import com.tencent.protocol.officialAccount.menu.create.MenuData;
import com.tencent.protocol.officialAccount.menu.get.MenuGetResData;
import org.springframework.stereotype.Component;

import javax.annotation.Resource;
import java.lang.reflect.InvocationTargetException;
import java.util.ArrayList;
import java.util.Comparator;
import java.util.Date;
import java.util.List;
import java.util.stream.Collectors;

/**
 * Created by LN on 2016/11/22.
 */
@Component("wxOfficialAccountsMenuFacadeService")
public class WxOfficialAccountsMenuFacadeServiceImpl extends BaseFacade implements IWxOfficialAccountsMenuFacadeService {
    @Resource
    private ICommonService commonServiceSupport;
    @Resource
    private IMenuService menuService;
    @Override
    public QueryResult<WxMenuResDto> getMenuResDto(WxMenuApiParamDto paramDto) throws Exception {
        String lhotelGroupCode = paramDto.getLhotelGroupCode();
        String isHalt = paramDto.getIsHalt();
        String inUse = paramDto.getInUse();
        if(StringUtil.isBlank(lhotelGroupCode)){
            throw new SysException(BaseSysExceptionEnum.INVALID_PARA.getCode(), BaseSysExceptionEnum.INVALID_PARA.getMsg()+"lhotelGroupCode is null", BaseSysExceptionEnum.INVALID_PARA.getBizType());
        }
        Integer currentPage = paramDto.getCurrentPage();
        if(currentPage == null){
            currentPage = 1;
        }
        Integer pageSize = paramDto.getPageSize();
        if(pageSize == null){
            pageSize = 5;
        }
        if(StringUtil.isBlank(isHalt)){
            isHalt = "F";
        }

        return getWxMenuResDto(lhotelGroupCode,null,isHalt,inUse,pageSize,currentPage);
    }



    @Override
    public void createMenu(WxMenuApiParamDto paramDto) throws Exception {
        String lhotelGroupCode = paramDto.getLhotelGroupCode();
        List<WxOfficialAccountsMenuDto> menu = paramDto.getMenu();
        if(StringUtil.isBlank(lhotelGroupCode) || menu == null ||menu.size() == 0){
            throw new SysException(BaseSysExceptionEnum.INVALID_PARA.getCode(), BaseSysExceptionEnum.INVALID_PARA.getMsg()+" lhotelGroupCode is null", BaseSysExceptionEnum.INVALID_PARA.getBizType());
        }
        checkMenuSize(menu);
        saveMenu(lhotelGroupCode,menu);
    }

    @Override
    public void updateMenu(WxMenuApiParamDto paramDto) throws Exception {
        List<WxOfficialAccountsMenuDto> menu = paramDto.getMenu();
        String lhotelGroupCode = paramDto.getLhotelGroupCode();
        checkMenuSize(menu);
        if(StringUtil.isBlank(lhotelGroupCode)){
            throw new SysException(BaseSysExceptionEnum.INVALID_PARA.getCode(), BaseSysExceptionEnum.INVALID_PARA.getMsg()+" lhotelGroupCode is null", BaseSysExceptionEnum.INVALID_PARA.getBizType());
        }
        if(menu == null || menu.size() == 0){
            throw new SysException(BaseSysExceptionEnum.INVALID_PARA.getCode(), BaseSysExceptionEnum.INVALID_PARA.getMsg()+" menu is empty", BaseSysExceptionEnum.INVALID_PARA.getBizType());
        }
        update(lhotelGroupCode,menu,paramDto.getDelectList());
    }

    @Override
    public void publishMenu(WxMenuApiParamDto paramDto) throws Exception {
        String menuCode = paramDto.getMenuCode();
        if(StringUtil.isBlank(menuCode)){
            throw new SysException(BaseSysExceptionEnum.INVALID_PARA.getCode(), BaseSysExceptionEnum.INVALID_PARA.getMsg()+" menuCode is null", BaseSysExceptionEnum.INVALID_PARA.getBizType());
        }
        QueryResult<WxMenuResDto> queryResult = getWxMenuResDto(null, menuCode, "F", null, 1, 0);
        if(queryResult != null && queryResult.getResultlist() != null &&queryResult.getResultlist().size() >0){
            WxMenuResDto wxMenuResDto = queryResult.getResultlist().get(0);
            String lhotelGroupCode = wxMenuResDto.getLhotelGroupCode();
            WxCertificate wx = getWxCertificate(lhotelGroupCode, null);
            MenuData data = getMenuData(wxMenuResDto);
            MenuCreateResData res = OfficialAccountClient.requsetCreateMenu(wx.getAccessToken(), data);
            if("0".equals(res.getErrcode())){
                //更新数据库
                List<WxOfficialAccountsMenu> updateEntitys = new ArrayList<>();
                convertNodes(wxMenuResDto.getMenu(),updateEntitys);
                updateEntitys.stream().forEach(entity -> entity.setInUse("T"));
                commonServiceSupport.saveOrUpdateList(updateEntitys);
            }else{
                throw new SysException(AeriabotsSysExceptionEnum.WX_SDK_ERROR.getCode(), AeriabotsSysExceptionEnum.WX_SDK_ERROR.getMsg()+" "+res.getErrcode()+","+res.getErrmsg(), AeriabotsSysExceptionEnum.WX_SDK_ERROR.getBizType());
            }
        }
    }

    private void checkMenuSize( List<WxOfficialAccountsMenuDto> menu ){
        if(menu == null || menu.size() == 0){
            throw new SysException(BaseSysExceptionEnum.INVALID_PARA.getCode(), BaseSysExceptionEnum.INVALID_PARA.getMsg()+" menu is empty", BaseSysExceptionEnum.INVALID_PARA.getBizType());
        }
        if(menu.size() > 3){
            throw new SysException(AeriabotsSysExceptionEnum.WX_ROOT_MENU_SIZE_TOO_BIG);
        }
        for (WxOfficialAccountsMenuDto root : menu) {
            if(root.getChildNodes()!=null && root.getChildNodes().size() > 5){
                throw new SysException(AeriabotsSysExceptionEnum.WX_CHILD_MENU_SIZE_TOO_BIG);
            }
        }
    }

    private MenuData getMenuData(WxMenuResDto wxMenuResDto) throws Exception {
        List<WxOfficialAccountsMenuDto> menu = wxMenuResDto.getMenu();
        if(menu != null &&  menu.size() >0 ){
            List<MenuButton> buttons = convertNodesForReq(menu);
            MenuData data = new MenuData();
            data.setButton(buttons);
            return data;
        }else{
            return null;
        }
    }

    private List<MenuButton> convertNodesForReq(List<WxOfficialAccountsMenuDto> menuNodes) throws Exception {
        if(menuNodes == null || menuNodes.size() == 0){
            return null;
        }
        List<MenuButton> button = new ArrayList<>();
        for (WxOfficialAccountsMenuDto menuDto : menuNodes) {
            MenuButton menuButton = new MenuButton();
            ClassConverUtil.copyProperties(menuDto,menuButton);
            menuButton.setSubButton(convertNodesForReq(menuDto.getChildNodes()));
            button.add(menuButton);
        }
        return button;
    }
    /**
     * 更新持久层 dao
     */
    private void update(String lhotelGroupCode ,List<WxOfficialAccountsMenuDto> menuNodes,List<Long> deletIds) throws Exception {
        if(menuNodes == null || menuNodes.size() ==0){
            return;
        }

        List<WxOfficialAccountsMenu> newEntitys = new ArrayList<>();
        List<WxOfficialAccountsMenu> updateEntitys = new ArrayList<>();
        String menuCode = "";
        //要更新的节点
        List<WxOfficialAccountsMenuDto> updateMenuDtos = menuNodes.stream()
                .filter(nodeDto -> nodeDto.getId() != null)
                .collect(Collectors.toList());

        if(updateMenuDtos != null && updateMenuDtos.size() >0){
            menuCode = updateMenuDtos.get(0).getMenuCode();
            for (WxOfficialAccountsMenuDto menuDto : updateMenuDtos) {
                WxOfficialAccountsMenu menuEntity = new WxOfficialAccountsMenu();
                ClassConverUtil.copyProperties(menuDto,menuEntity);
                //要更新的root节点
                updateEntitys.add(menuEntity);
                //子节点
                List<WxOfficialAccountsMenuDto> childNodes = menuDto.getChildNodes();
                if(childNodes != null && childNodes.size() >0){
                    //新增的子节点
                    List<WxOfficialAccountsMenuDto> newChildren = childNodes.stream()
                            .filter(childNode -> childNode.getId() == null)
                            .collect(Collectors.toList());
                    if(newChildren != null && newChildren.size() >0){
                        for (WxOfficialAccountsMenuDto child : newChildren) {
                            WxOfficialAccountsMenu newChildEntity = createChildEntity(lhotelGroupCode,menuCode,child,menuEntity.getNodeCode());
                            newEntitys.add(newChildEntity);
                        }
                    }
                    //更新的子节点
                    List<WxOfficialAccountsMenuDto> updateChildren = childNodes.stream()
                            .filter(childNode -> childNode.getId() != null)
                            .collect(Collectors.toList());
                    if(updateChildren != null && updateChildren.size() >0){
                        for (WxOfficialAccountsMenuDto updateChild : updateChildren) {
                            WxOfficialAccountsMenu childEntity = new WxOfficialAccountsMenu();
                            ClassConverUtil.copyProperties(updateChild,childEntity);
                            updateEntitys.add(childEntity);
                        }
                    }
                }

            }
        }else{
            menuCode = createMenuCode(lhotelGroupCode);
        }

        //新的节点
        List<WxOfficialAccountsMenuDto> rootMenuDtos = menuNodes.stream().filter(nodeDto -> nodeDto.getId() == null)
                .filter(nodeDto2 ->WxOfficialAccountsMenu._NodeType.rootNote.equals(nodeDto2.getNodeType()))
                .collect(Collectors.toList());


         //父节点
        for (WxOfficialAccountsMenuDto dto : rootMenuDtos) {
            WxOfficialAccountsMenu rootEntity = createRootEntity(lhotelGroupCode, menuCode, dto);
            newEntitys.add(rootEntity);
            List<WxOfficialAccountsMenuDto> childCodes = dto.getChildNodes();
            //子节点
            if(childCodes != null && childCodes.size() >0){
                for (WxOfficialAccountsMenuDto childDto : childCodes) {
                    WxOfficialAccountsMenu childEntity = createChildEntity(lhotelGroupCode, menuCode, childDto, rootEntity.getNodeCode());
                    newEntitys.add(childEntity);
                }
            }
        }
        //持久化
        updateEntitys.addAll(newEntitys);
        if(updateEntitys != null && updateEntitys.size() > 0){
            menuService.updateMenu(updateEntitys,deletIds);
        }
    }

    /**
     * entity和dto转换
     * @param menuNodes
     * @param updateEntitys
     * @throws Exception
     */
    private void convertNodes(List<WxOfficialAccountsMenuDto> menuNodes , List<WxOfficialAccountsMenu> updateEntitys) throws Exception {
        if(menuNodes == null || menuNodes.size() == 0){
            return ;
        }
        for (WxOfficialAccountsMenuDto menuDto : menuNodes) {
            WxOfficialAccountsMenu menuEntity = new WxOfficialAccountsMenu();
            ClassConverUtil.copyProperties(menuDto,menuEntity);
            updateEntitys.add(menuEntity);
            convertNodes(menuDto.getChildNodes(),updateEntitys);
        }
        return ;
    }


    private String createMenuCode(String lhotelGroupCode){
        return lhotelGroupCode + DefaultValue.MARK +System.currentTimeMillis();
    }
    private void saveMenu(String lhotelGroupCode ,  List<WxOfficialAccountsMenuDto> menuNodes) throws Exception {
        if(menuNodes == null || menuNodes.size() == 0){
            return;
        }
        String menuCode = createMenuCode(lhotelGroupCode);
        List<WxOfficialAccountsMenu> menuEntitys = new ArrayList<>();
        for (int i = 0;i<menuNodes.size();i++) {

            WxOfficialAccountsMenuDto menuDto = menuNodes.get(i);
            //没有排序则默认按list顺序排序
            if(menuDto.getListOrder() == null){
                menuDto.setListOrder(i);
            }
            WxOfficialAccountsMenu rootEntity = createRootEntity(lhotelGroupCode, menuCode ,menuDto);
            menuEntitys.add(rootEntity);
            //子节点创建
            List<WxOfficialAccountsMenuDto> childNodes = menuDto.getChildNodes();
            if(childNodes != null && childNodes.size() > 0){
                for (int j = 0;j < childNodes.size() ; j ++) {
                    WxOfficialAccountsMenuDto childDto = childNodes.get(j);
                    if(childDto.getListOrder() == null){
                        childDto.setListOrder(j);
                    }
                    WxOfficialAccountsMenu childEntity = createChildEntity(lhotelGroupCode, menuCode, childDto,rootEntity.getNodeCode());
                    menuEntitys.add(childEntity);
                }
            }
        }
        commonServiceSupport.saveList(menuEntitys);
    }

    /**
     * 创建childNodeEntity
     * @param lhotelGroupCode
     * @param menuCode
     * @param childDto
     * @param parentNode
     * @return
     * @throws Exception
     */
    private WxOfficialAccountsMenu createChildEntity(String lhotelGroupCode , String menuCode , WxOfficialAccountsMenuDto childDto, String parentNode) throws Exception{
        WxOfficialAccountsMenu childEntity = new WxOfficialAccountsMenu();
        ClassConverUtil.copyProperties(childDto,childEntity);
        childEntity.setLhotelGroupCode(lhotelGroupCode);
        childEntity.setMenuCode(menuCode);
        childEntity.setParentNode(parentNode);
        childEntity.setInUse("T");
        childEntity.setNodeType(WxOfficialAccountsMenu._NodeType.subNode);
        childEntity.setNodeCode("nodeCode_"+System.nanoTime());
        return childEntity;
    }
    /**
     * 创建rootNodeEntity
     * @param lhotelGroupCode
     * @param menuCode
     * @param menuDto
     * @return
     * @throws Exception
     */
    private WxOfficialAccountsMenu createRootEntity(String lhotelGroupCode, String menuCode, WxOfficialAccountsMenuDto menuDto) throws Exception{
        WxOfficialAccountsMenu rootMenuEntity = new WxOfficialAccountsMenu();
        ClassConverUtil.copyProperties(menuDto,rootMenuEntity);
        rootMenuEntity.setLhotelGroupCode(lhotelGroupCode);
        rootMenuEntity.setNodeType(WxOfficialAccountsMenu._NodeType.rootNote);
        rootMenuEntity.setInUse("T");
        rootMenuEntity.setMenuCode(menuCode);
        rootMenuEntity.setNodeCode("nodeCode_"+System.nanoTime());
        return rootMenuEntity;
    }

    private QueryResult<WxMenuResDto> getWxMenuResDto(String lhotelGroupCode,String cMenuCode ,String isHalt, String inUse, Integer pageSize , Integer currentPage) throws Exception {
        WxOfficialAccountsMenu findMenuCode = new WxOfficialAccountsMenu();
        findMenuCode.setLhotelGroupCode(lhotelGroupCode);
        findMenuCode.setIsHalt(isHalt);
        findMenuCode.setInUse(inUse);
        findMenuCode.setMenuCode(cMenuCode);
        QueryResult<WxOfficialAccountsMenu> queryResult = commonServiceSupport.findAllEQ(WxOfficialAccountsMenu.class, findMenuCode,currentPage,pageSize,null,new String[]{WxOfficialAccountsMenu.WxOfficialAccountsMenu_.menuCode.toString()});
        List<WxOfficialAccountsMenu> resultList = queryResult.getResultlist();
        //本地没有 查询没有结果 请求微信接口
        if(resultList == null || resultList.size() == 0){
            //查询没有结果 请求微信接口
            WxMenuResDto wxMenuResDto = requestFromWx(lhotelGroupCode);

            if(wxMenuResDto==null || wxMenuResDto.getMenu() ==null || wxMenuResDto.getMenu().size() == 0){
                QueryResult result = new QueryResult();
                result.setCurrentPage(queryResult.getCurrentPage());
                result.setTotalrecord(0);
                return result;
            }else{
                //保存
                saveMenu(lhotelGroupCode,wxMenuResDto.getMenu());
                return getWxMenuResDto(lhotelGroupCode,null ,null,null, 1 , 1);
            }
        }else {
            return buildResultDto(resultList,queryResult.getCurrentPage(),queryResult.getTotalrecord());
        }
    }


    /**
     * 组装返回结果
     * @param entitys
     * @param currentPage
     * @param totalrecord
     * @return
     * @throws Exception
     */
    private QueryResult<WxMenuResDto> buildResultDto(List<WxOfficialAccountsMenu> entitys,Integer currentPage ,Long totalrecord) throws Exception {
            QueryResult<WxMenuResDto> result = new QueryResult();
            List<WxMenuResDto> menuList = new ArrayList<>();
            result.setCurrentPage(currentPage);
            result.setTotalrecord(totalrecord);
            //menuCode循环
            for (WxOfficialAccountsMenu menuCodeEntity : entitys) {
                String menuCode = menuCodeEntity.getMenuCode();
                WxOfficialAccountsMenu find = new WxOfficialAccountsMenu();
                find.setMenuCode(menuCode);
                find.setLhotelGroupCode(menuCodeEntity.getLhotelGroupCode());
                QueryResult<WxOfficialAccountsMenu> menuEntityRes = commonServiceSupport.findAllEQ(WxOfficialAccountsMenu.class, find);
                if(menuEntityRes != null && menuEntityRes.getResultlist() != null && menuEntityRes.getResultlist().size() > 0){
                    List<WxOfficialAccountsMenuDto> resultDto = new ArrayList<>();

                    List<WxOfficialAccountsMenu> menuEntitys = menuEntityRes.getResultlist();
                    //选出根节点
                    List<WxOfficialAccountsMenu> rootEntitys = menuEntitys.stream()
                            .filter(menuEntity -> WxOfficialAccountsMenu._NodeType.rootNote.equals(menuEntity.getNodeType()))
                            .collect(Collectors.toList());
                    if(rootEntitys != null && rootEntitys.size() > 0){
                        for (WxOfficialAccountsMenu rootEntity : rootEntitys) {
                            WxOfficialAccountsMenuDto rootDto = new WxOfficialAccountsMenuDto();
                            ClassConverUtil.copyProperties(rootEntity,rootDto);
                            //筛选子节点
                            List<WxOfficialAccountsMenu> childEntitys = menuEntitys.stream()
                                    .filter(e -> rootEntity.getNodeCode().equals(e.getParentNode()))
                                    .filter(menuEntity -> WxOfficialAccountsMenu._NodeType.subNode.equals(menuEntity.getNodeType()))
                                    .sorted(Comparator.comparing(WxOfficialAccountsMenu::getListOrder))
                                    .collect(Collectors.toList());
                            if(childEntitys != null && childEntitys.size() > 0){
                                List<WxOfficialAccountsMenuDto> childDtos = new ArrayList<>();
                                for (WxOfficialAccountsMenu childEntity : childEntitys) {
                                    WxOfficialAccountsMenuDto childDto = new WxOfficialAccountsMenuDto();
                                    ClassConverUtil.copyProperties(childEntity,childDto);
                                    childDtos.add(childDto);
                                }
                                rootDto.setChildNodes(childDtos);
                            }
                            resultDto.add(rootDto);
                        }
                    }
                    WxMenuResDto menu = new WxMenuResDto();
                    menu.setInUse(menuCodeEntity.getInUse());
                    menu.setMenuCode(menuCodeEntity.getMenuCode());
                    menu.setMenu(resultDto);
                    menu.setLhotelGroupCode(menuCodeEntity.getLhotelGroupCode());
//                    menu.setName("menuName");
                    menuList.add(menu);
                }
            }
            result.setResultlist(menuList);
            return result;
    }




    private WxMenuResDto requestFromWx(String lhotelGroupCode) throws Exception {
        WxCertificate wc = getWxCertificate(lhotelGroupCode, null);
        String accessToken = wc.getAccessToken();
        MenuGetResData requestResData = OfficialAccountClient.requestGetMenu(accessToken);
        if("46003".equals(requestResData.getErrcode())){
            throw new SysException(AeriabotsSysExceptionEnum.WX_SDK_ERROR.getCode()
                    , AeriabotsSysExceptionEnum.WX_SDK_ERROR.getMsg()+"[46003] 不存在的菜单数据"
                    , AeriabotsSysExceptionEnum.WX_SDK_ERROR.getBizType());
        }
        if(StringUtil.isNotBlank(requestResData.getErrcode())){
            throw new SysException(AeriabotsSysExceptionEnum.WX_SDK_ERROR.getCode()
                    , AeriabotsSysExceptionEnum.WX_SDK_ERROR.getMsg()+"[Errcode]"+requestResData.getErrcode()+" [Errmsg]"+requestResData.getErrmsg()
                    , AeriabotsSysExceptionEnum.WX_SDK_ERROR.getBizType());
        }
        MenuData menu = requestResData.getMenu();
        if(menu == null){
            throw new SysException(AeriabotsSysExceptionEnum.WX_SDK_ERROR.getCode()
                    , AeriabotsSysExceptionEnum.WX_SDK_ERROR.getMsg()+"不存在的菜单数据"
                    , AeriabotsSysExceptionEnum.WX_SDK_ERROR.getBizType());
        }
        List<MenuButton> buttons = menu.getButton();
        if(buttons == null || buttons.size() == 0){
            throw new SysException(AeriabotsSysExceptionEnum.WX_SDK_ERROR.getCode()
                    , AeriabotsSysExceptionEnum.WX_SDK_ERROR.getMsg()+"不存在的菜单数据"
                    , AeriabotsSysExceptionEnum.WX_SDK_ERROR.getBizType());
        }
        WxMenuResDto wxMenuResDto = new WxMenuResDto();
        wxMenuResDto.setLhotelGroupCode(lhotelGroupCode);
        List<WxOfficialAccountsMenuDto> nodes = button2Node(buttons);
        wxMenuResDto.setMenu(nodes);
        return wxMenuResDto;
    }

    List<WxOfficialAccountsMenuDto> button2Node( List<MenuButton> buttons) throws InvocationTargetException, IllegalAccessException {
        if(buttons == null || buttons.size() == 0){
            return null;
        }
        List<WxOfficialAccountsMenuDto> nodeList = new ArrayList<>();
        for (MenuButton button : buttons) {
            WxOfficialAccountsMenuDto rootNode = new WxOfficialAccountsMenuDto();
            ClassConverUtil.copyProperties(button,rootNode);
            List<MenuButton> subButtons = button.getSubButton();
            List<WxOfficialAccountsMenuDto> childNodes = button2Node(subButtons);
            rootNode.setChildNodes(childNodes);
            nodeList.add(rootNode);
        }
        return nodeList;
    }
    @Override
    public WxJspSignApiDto requestWxJsSignDto(WxJspSignParamDto paramDto) throws Exception {
        {
            if(StringUtil.isBlank(paramDto.getLhotelGroupCode()) || StringUtil.isBlank(paramDto.getUrl())){
                throw new SysException(BaseSysExceptionEnum.INVALID_PARA.getCode(), BaseSysExceptionEnum.INVALID_PARA.getMsg()+"lhotelGroupCode : "+paramDto.getLhotelGroupCode()+"\n url : "+paramDto.getUrl(),
                        BaseSysExceptionEnum.INVALID_PARA.getBizType());
            }
            WxCertificate wc = this.getWxCertificate(paramDto.getLhotelGroupCode(), null);
            String timestamp =String.valueOf((System.currentTimeMillis()) / 1000);
            String nonce = Integer.toString(timestamp.hashCode());
            String ticket = wc.getJspTicket();
            String appid = wc.getAppid();
            String sign = SHA1.SHA1(new WxJspSign(ticket,nonce,timestamp,paramDto.getUrl()));
            WxJspSignApiDto dto = new WxJspSignApiDto();
            dto.setAppid(appid);
            dto.setNonceStr(nonce);
            dto.setTimestamp(timestamp);
            dto.setSign(sign);
            return dto;
        }
    }
    private static class WxJspSign{
        public WxJspSign(String jsapi_ticket,String noncestr,String timestamp,String url){
            this.jsapi_ticket =jsapi_ticket;
            this.noncestr = noncestr;
            this.timestamp = timestamp;
            this.url = url;
        }
        private String jsapi_ticket;
        private String noncestr;
        private String timestamp;
        private String url;

        public String getJsapi_ticket() {
            return jsapi_ticket;
        }

        public void setJsapi_ticket(String jsapi_ticket) {
            this.jsapi_ticket = jsapi_ticket;
        }

        public String getNoncestr() {
            return noncestr;
        }

        public void setNoncestr(String noncestr) {
            this.noncestr = noncestr;
        }

        public String getTimestamp() {
            return timestamp;
        }

        public void setTimestamp(String timestamp) {
            this.timestamp = timestamp;
        }

        public String getUrl() {
            return url;
        }

        public void setUrl(String url) {
            this.url = url;
        }
    }
}
